package com.timeset.photo.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.qiniu.common.QiniuException;
import com.timeset.activity.entity.Student;
import com.timeset.photo.entity.Photo;
import com.timeset.photo.entity.PhotoJson;
import com.timeset.photo.entity.PhotoList;
import com.timeset.photo.service.PhotoServiceImpl;
import com.timeset.user.controller.UserController;
import com.timeset.user.service.UserServiceImpl;
import com.timeset.util.Constant;
import com.timeset.util.MultipartFileToFileUtil;
import com.timeset.util.QiniuUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName PhotoController
 * @Description
 * @Author lz
 * @Date 2020-04-23 18:06
 */
@RestController
@RequestMapping("/photo")
public class PhotoController {
    List<Photo> photosList = null;

    @Resource
    private PhotoServiceImpl photoService;
    @Resource
    private QiniuUtil qiniuUtil;
    @Resource
    private UserServiceImpl userService;


    //上传图片到七牛云
    //更新user头像，认证照片
    @RequestMapping("/updateUserImgPath")
    public Map updateOrgHeadPath(@RequestParam("file") MultipartFile files, @RequestParam("userId") int userId, @RequestParam("role") int r, @RequestParam("who") int who) {
        int w = who;
        int role = r;
        Map map=new HashMap();
        if(w==1){
            map.put("who","one");
        } if(w==2){
            map.put("who","two");
        }
        if(w==3){
            map.put("who","three");
        }


        MultipartFile file = files;
        String fileName = file.getOriginalFilename();
        System.out.println("imgName" + fileName);
        File f = null;
        String path = "";
        if (file != null) {
            try {
                f = MultipartFileToFileUtil.multipartFileToFile(file);
                path = qiniuUtil.saveImage(f, fileName);
                System.out.println("path1" + path);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        MultipartFileToFileUtil.delteTempFile(f);

        if (role == 0) {//是组织
            System.out.println("path2" + path);
            int i;
            if (path != null) {
                if (w == 1) {//进行头像的更新操作
                    //查找原先的头像地址进行删除：
                    String yPath = userService.findOrganizationById(userId).getHeadPath();
                    System.out.println("原先的头像地址" + yPath);
                    i = photoService.updateOrgImgPath(path, userId);//更新操作
                    if (i==1) {
                        if (yPath!=null&&yPath.length() > 0) {//删除原头像
                            try {
                                qiniuUtil.delete(yPath);
                            } catch (QiniuException e) {
                                e.printStackTrace();
                            }
                        }
                        map.put("result","ok");
                    }else {
                        map.put("result","no");
                    }

                    return map;

                }
            } else {
                return null;
            }

        }else if (role == 1) {//是学生
            System.out.println("path3" + path);
            int s;
            if (path != null) {
                if (w == 1) {//进行头像的更新操作
                    //查找原先的头像地址进行删除：
                    String stuHeadPath=userService.findStudentById(userId).getHeadPath();
                    System.out.println("原先的头像地址"+stuHeadPath);
                    s=photoService.updateStuHeadPath(userId,path);
                    if (s==1) {
                        if (stuHeadPath!=null&&stuHeadPath.length() > 0) {//删除原头像
                            try {
                                qiniuUtil.delete(stuHeadPath);
                            } catch (QiniuException e) {
                                e.printStackTrace();
                            }
                        }
                        map.put("result","ok");
                    }else{
                        map.put("result","no");
                    }
                    return map;
                } else if (w == 2 || w==3) {//认证图片 2正面 3 反面


                    if(w==2){
                        String  rePath=userService.findStudentById(userId).getIdentityImgObverse();
                        int re=photoService.updateStuObverse(userId,path);
                        System.out.println("原先证件图片地址1" + rePath);
                        if (re==1){
                            //删除原先的图片
                            if (rePath!=null && rePath.length() > 0) {
                                try {
                                    qiniuUtil.delete(rePath);
                                } catch (QiniuException e) {
                                    e.printStackTrace();
                                }
                            }
                            //将认证状态设为0，重新认证
                            //修改为未认证
                            int k = userService.updateStuIdentityById(userId, 0);
                            System.out.println("修改状态：：：：：：" + k);
                            map.put("result","ok");
                        }else {
                            map.put("result","no");
                        }

                    }
                    if(w==3){
                        Student student=userService.findStudentById(userId);
                        System.out.println(student.toString());
                        String  reP=student.getIdentityImgReverse();
                        int result01=photoService.updateStuReverse(userId,path);
                        System.out.println("原先证件图片地址" + reP);
                        if (result01==1){
                            //删除原先的图片
                            if (reP!=null && reP.length() > 0) {
                                try {
                                    qiniuUtil.delete(reP);
                                } catch (QiniuException e) {
                                    e.printStackTrace();
                                }
                            }
                            //将认证状态设为0，重新认证
                            //修改为未认证
                            int k = userService.updateStuIdentityById(userId, 0);
                            System.out.println("修改状态：：：：：：" + k);
                            map.put("result","ok");
                        }else {
                            map.put("result","no");
                        }

                    }



                    return map;
                }
            } else {
                return null;
            }
        }else{//既不是学生也不是组织
            return null;
        }
        System.out.println(map.toString());
        return null;
    }











}
